/*
 * Copyright (C) 2014 Steven Kurilovitch
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.goodjaerb.swccg.core;

/**
 *
 * @author Steven Kurilovitch
 */
public class DeckListItem {
    
    private final CardData cd;
    private int count;
    private int startingCount;
    private boolean starting;
    
    private int listIndex; // Sorta awkward but this helps with drag-n-drop
    
    public DeckListItem(CardData cd) {
        this(cd, 1, 0);
    }
    
    public DeckListItem(CardData cd, int count) {
        this(cd, count, 0);
    }
    
    public DeckListItem(CardData cd, int count, int startingCount) {
        this.cd = cd;
        this.count = count;
        this.startingCount = startingCount;
        this.starting = (startingCount > 0);
    }
    
    public void setListIndex(int index) {
        this.listIndex = index;
    }
    
    public int getListIndex() {
        return listIndex;
    }
    
    public void incrementCount() {
        setCount(getCount() + 1);
    }
    
    public void decrementCount() {
        setCount(getCount() - 1);
    }
    
    public void setCount(int c) {
        if(c < 0) {
            c = 0;
        }
        this.count = c;
        if(getCount() < getStartingCount()) {
            setStartingCount(getCount());
        }
    }
    
    public int getCount() {
        return count;
    }
    
    public void incrementStartingCount() {
        setStartingCount(getStartingCount() + 1);
    }
    
    public void decrementStartingCount() {
        setStartingCount(getStartingCount() - 1);
    }
    
    public void setStartingCount(int c) {
        if(c < 0) {
            c = 0;
        }
        
        this.startingCount = c;
        if(getStartingCount() == 0) {
            this.starting = false;
        }
        else {
            this.starting = true;
            if(getStartingCount() > getCount()) {
                setCount(getStartingCount());
            }
        }
    }
    
    public int getStartingCount() {
        return startingCount;
    }
    
    public void toggleStarting() {
        setStarting(!isStarting());
    }
    
    public void setStarting(boolean b) {
        this.starting = b;
        if(isStarting()) {
            if(getStartingCount() < 1) {
                this.startingCount = 1;
            }
        }
        else {
            this.startingCount = 0;
        }
    }
    
    public boolean isStarting() {
        return starting;
    }
    
    public CardData getCardData() {
        return cd;
    }
    
    @Override
    public boolean equals(Object o) {
        if(o instanceof DeckListItem) {
            return cd.equals(((DeckListItem)o).getCardData());
        }
        return false;
    }
    
    @Override
    public String toString() {
        String s = "";
        if(isStarting()) {
            s += "(S";
            if(getStartingCount() > 1) {
                s += "x" + getStartingCount();
            }
            s += ") ";
        }
        if(getCount() > 1) {
            s += getCount() + "x ";
        }
        return s += getCardData().get(CardData.Field.TITLE) + " (" + getCardData().get(CardData.Field.SET) + ")";
    }
}
